﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using University.Workshop.Model;

namespace University.Workshop.DataAccess
{
    public static class RentDataAccess
    {
        const string ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=Pharmacy;Integrated Security=True";
        
        public static DataTable GetRentCode(string nothing)
        {
            string sqlSentence = "SELECT MAX(CAST(CodeRent AS BigInt)) FROM Rent";
            if (nothing == null)
                throw new BusinessException("nothing null");

            SqlConnection cnn = null;
            SqlConnection tempCnn = null;

            DataTable table = new DataTable();
            try
            {
                table.Locale = System.Globalization.CultureInfo.CurrentCulture;
                tempCnn = new SqlConnection(ConnectionString);
                cnn = tempCnn;
                cnn.Open();
                SqlCommand cmd = tempCnn.CreateCommand();
                cmd.CommandText = sqlSentence;
                cmd.CommandType = System.Data.CommandType.Text;
                SqlDataReader reader = cmd.ExecuteReader();
                table.Load(reader);
                return table;
            }
            finally
            {
                if (table != null)
                {
                    table.Dispose();
                }
                if (tempCnn != null)
                    tempCnn.Close();
            } 
        }

        public static bool RentRegister(Rent rent)
        {
            string sqlSentence = "INSERT INTO Rent VALUES(@Code,@CodeUser,@DateRent,@Concept,@Category,@TotalAmount)";
            SqlConnection cnn = null;
            SqlConnection tempCnn = null;
            int result = 0;
            if (rent == null)
                throw new BusinessException("nothing null");
            try
            {
                tempCnn = new SqlConnection(ConnectionString);
                cnn = tempCnn;
                cnn.Open();
                SqlCommand cmd = tempCnn.CreateCommand();
                cmd.CommandText = sqlSentence;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Parameters.AddWithValue("@Code", rent.CodeRent);
                cmd.Parameters.AddWithValue("@CodeUser", rent.CodeUser);
                cmd.Parameters.AddWithValue("@DateRent", rent.CurrentDate);
                cmd.Parameters.AddWithValue("@Concept", rent.Concept);
                cmd.Parameters.AddWithValue("@Category", rent.Category);
                cmd.Parameters.AddWithValue("@TotalAmount", Convert.ToDouble(rent.TotalAmount, System.Globalization.CultureInfo.CurrentCulture));
                result = cmd.ExecuteNonQuery();
                if (result == 0)
                    return false;
                else
                    return true;
            }
            finally
            {
                if (tempCnn != null)
                    tempCnn.Close();
            } 
        }

        public static bool RentDetailRegister(RentDetail rentDetail)
        {
            string sqlSentence = "INSERT INTO RentDetail VALUES(@CodeRent,@Amount,@Note)";
            SqlConnection cnn = null;
            SqlConnection tempCnn = null;
            int result = 0;
            if (rentDetail == null) {
                throw new BusinessException("Se quiso ingresar un detalle nulo");
            }
            try
            {
                tempCnn = new SqlConnection(ConnectionString);
                cnn = tempCnn;
                cnn.Open();
                SqlCommand cmd = tempCnn.CreateCommand();
                cmd.CommandText = sqlSentence;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Parameters.AddWithValue("@CodeRent", rentDetail.CodeRent);
                cmd.Parameters.AddWithValue("@Amount", rentDetail.Amount);
                cmd.Parameters.AddWithValue("@Note", rentDetail.Note);
                result = cmd.ExecuteNonQuery();
                if (result == 0)
                    return false;
                else
                    return true;
            }
            finally
            {
                if (tempCnn != null)
                    tempCnn.Close();
            } 
        }
    }
}
